<?php


require_once("Controller/UserFileController.php");
require_once("Controller/AdministrationController.php");
require_once("view/UserView.php");

class ShowUserController{
	
	private $usersView = null;
	private $usersHandler = null;
	private $userFileController = null;
	private $aministrationController = null;
	
	
	public function __construct($a_userHandler, DBConnection $a_DBConnection, $a_DBPrefix){
		$this->usersView = new UserView();
		$this->usersHandler = $a_userHandler;
		$this->userFileController = new UserFileController($a_DBConnection, $a_DBPrefix);
		$this->aministrationController = new AdministrationController();
	}
	
	public function doControll($a_loggedInUser){
		$page = new Page();
		$userIdToRemove = false;
		$result;
		
		//If ADMIN
		if($a_loggedInUser->IsAdmin){
			return $this->aministrationController->doControll($this->usersHandler, $this->userFileController);
		}
		
		//If NOT ADMIN
		//Did user change password?
		if($this->usersView->didUserChangePassword() === true){
			$newPass = $this->usersView->getNewPassword();
			if($newPass !== false){
				$updatedUser = $this->usersHandler->updatePasswordOnUser($a_loggedInUser, $newPass);
				$this->usersView->setNewPasswordMessage($updatedUser);
			}
			else{
				$this->usersView->setNewPasswordMessage();
			}
			
		}
		
		//check if there is an ID in querystring
		$isActiveUser = NavigationView::getActiveUser();
		$activeUser = $this->usersHandler->getUserByuserId($isActiveUser);
		
		//If no Id, or an incorrect id, display all users
		if($isActiveUser === false OR $activeUser === false){
			$page->body = $this->usersView->generateUserList($this->usersHandler->getAllUsers());
			$page->title = " | All Users";
		}
		//If Id, Display user
		else{
			$page->body = $this->usersView->showUser($a_loggedInUser, $activeUser);
			$page->title = " | " . $activeUser->UserName;
			
			$newPage = $this->userFileController->DoControll($a_loggedInUser, $isActiveUser);
			$page->body .= $newPage->body;
		}
		
		return $page;
	}
}